package com.sunchangpeng.wechat.mp.stat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunchangpeng.wechat.core.WechatClient;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public abstract class Stats {
    protected static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    protected WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    protected boolean diffThanDays(LocalDate start, LocalDate end, int diff) {
        return start.plusDays(diff).isBefore(end);
    }

    protected String doRequest(String url, LocalDate start, LocalDate end) {
        return wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject()
                .fluentPut("begin_date", start.format(formatter))
                .fluentPut("end_date", end.format(formatter))));
    }
}
